package com.hc.common.utils.encode;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Base64Util {
    public static String encode(byte[] src) {
        byte[] encodeBytes = Base64.getEncoder().encode(src);
        return new String(encodeBytes);
    }
  
    public static String encode(String src) {
        byte[] encodeBytes = Base64.getEncoder().encode(src.getBytes());
        return new String(encodeBytes);
    }

    public static String decode(String src) {
        byte[] decodeBytes = Base64.getDecoder().decode(src.getBytes());
        return new String(decodeBytes);
    }

    public static String encode(String src, String charset) throws UnsupportedEncodingException {
        byte[] encodeBytes = Base64.getEncoder().encode(src.getBytes(charset));
        return new String(encodeBytes);
    }

    public static String decode(String src, String charset) throws UnsupportedEncodingException {
        byte[] decodeBytes = Base64.getDecoder().decode(src.getBytes());
        return new String(decodeBytes,charset);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String temp1 = encode("12345678");
        System.out.println(temp1); //5Lit5Zu9
        String res1 = decode(temp1);
        System.out.println(res1); //中国

        String temp2 = encode("中国","utf-8");
        System.out.println(temp2); //5Lit5Zu9
        String res2 = decode(temp2,"utf-8");
        System.out.println(res2); //中国

        String temp3 = encode("中国","gbk");
        System.out.println(temp3); //1tC5+g==
        String res3 = decode(temp3,"gbk");
        System.out.println(res3); //中国
    }

}
